package com.xuecheng.web.exception;

//异常处理器

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice //将异常处理器加到容器
@Slf4j
public class GlobalExceptionHandler {

    //处理公共异常
    @ExceptionHandler(RuntimeException.class)
    public ResponseResult handerRuntimeException(RuntimeException e){
        log.error(e.getMessage());
        return ResponseResult.errorResult(ErrorCode.ERROR);
    }
    //处理自定义异常
    @ExceptionHandler(BusinessException.class)
    public ResponseResult handerBusinessException(BusinessException e){
        ErrorCode code = e.getCode();
        return ResponseResult.errorResult(code);
    }
}
